import 'dart:math';
import 'dart:ui';

import 'package:flutterlib/common/constants/constants.dart';

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return LoginPageState();
    // TODO: implement createState
    throw UnimplementedError();
  }

}

class LoginPageState extends State<LoginPage> with TickerProviderStateMixin {
  List<BobbleBean> _list = [];
  Random _random = Random(DateTime
      .now()
      .millisecondsSinceEpoch);

  Color getRandomWhiteColor(Random random) {
    int a = random.nextInt(200);

    return Color.fromARGB(a, 255, 255, 255);
  }

  double _maxSpeed = 1.0;
  double _maxRadius = 100;
  double _maxThte = 2 * pi;
  AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    for (var i = 0; i < 20; ++i) {
      BobbleBean bean = BobbleBean();
      bean.color = getRandomWhiteColor(_random);
      bean.postion = Offset(-1, -1);
      bean.speed = _random.nextDouble() * _maxSpeed;
      bean.radius = _random.nextDouble() * _maxRadius;
      bean.theta = _random.nextDouble() * _maxThte;
      _list.add(bean);
    }
    _animationController = AnimationController(
      vsync: this,
      duration: Duration(
          microseconds: 1000
      ),
    );
    _animationController.addListener(() {
      setState(() {});
    });
    _animationController.repeat();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: QuhuiColors.white,
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: Stack(
          children: [
            buildBackGround(),
            buildBobbleWidget(context),
            buildBlurWidget(),
            buildTopTextWidget(),
            buildButtomColumn(),
          ],
        ),
      ),
    );
  }


  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  buildBackGround() {
    return Container(
      decoration: BoxDecoration(
          gradient: LinearGradient(
              colors: [
                Colors.lightBlueAccent.withOpacity(0.3),
                Colors.lightBlue.withOpacity(0.3),
                Colors.blue.withOpacity(0.3)
              ]
          )
      ),
    );
  }

  buildBobbleWidget(BuildContext context) {
    return CustomPaint(
      size: MediaQuery
          .of(context)
          .size,
      painter: CustomMyPainter(
        list: _list,
        random: _random,


      ),
    );
  }

  buildBlurWidget() {
    return BackdropFilter(
      filter: ImageFilter.blur(sigmaX: 0.3, sigmaY: 0.3),
      child: Container(
        color: QuhuiColors.white.withOpacity(0.1),
      ),
    );
  }

  buildTopTextWidget() {
    return Positioned(
      left: 0,
      right: 0,
      top: 160,
      child: Text("Hello World", style: TextStyle(
        fontSize: 44,
        color: Colors.deepPurple,

      ),
        textAlign: TextAlign.center,
      ),
    );
  }

  buildButtomColumn() {
    return Positioned(
      bottom: 44,
      left: 0,
      right:0,
      child:  Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          TextFieldWidget(
            obscureText: false,
            labelText: "账号",
            prefixIconData: Icons.phone_android,

          ),
          SizedBox(
            height: 14,
          ),
          TextFieldWidget(
            obscureText: true,
            labelText: "密码",
            suffixIconData: Icons.visibility,
            prefixIconData: Icons.lock_outline,
          ),
          SizedBox(height: 14,),
          Text("忘记密码" , textAlign: TextAlign.end, style: TextStyle(fontSize: 14, color: Colors.blue),),
          SizedBox(height: 24,),
          RaisedButton(child: Text("登录"), onPressed: (){},),
          SizedBox(height: 14,),
          Container(
            height: 42,
            width: double.infinity,
            child: RaisedButton(
              onPressed: (){},
              child: Text("注册"),
            ),
          )




        ],
      ),
    );
  }
}

typedef onChange = Function(String value);
class TextFieldWidget extends StatelessWidget {

  onChange change;
  bool obscureText;
  String labelText;

  IconData prefixIconData;

  IconData suffixIconData;


  @override
  Widget build(BuildContext context) {
    return TextField(
      onChanged: change,
      obscureText: obscureText,

      style: TextStyle(
        color: Colors.blue,
        fontSize: 14.0,
      ),
      decoration: InputDecoration(
        filled: true,
        labelText: labelText,
        enabledBorder: UnderlineInputBorder(
          borderSide: BorderSide.none,
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(color: Colors.blue),
        ),
          prefixIcon: Icon(
            prefixIconData,
            size: 18,
            color: Colors.blue,

      ),
          suffixIcon: Icon(
            suffixIconData,
            size: 18,
            color: Colors.blue,

      )
      ),


    );
  }


  TextFieldWidget({this.change, this.labelText, this.obscureText, this.prefixIconData,
      this.suffixIconData});


}


class CustomMyPainter extends CustomPainter {
  List<BobbleBean> list;
  Random random;


  CustomMyPainter({this.list, this.random});

  Paint _paint = Paint()
    ..isAntiAlias = true;

  @override
  void paint(Canvas canvas, Size size) {
    list.forEach((element) {
      Offset newCenterOffset = calculateXY(element.speed, element.theta);
      double dx = newCenterOffset.dx + element.postion.dx;
      double dy = newCenterOffset.dy + element.postion.dy;

      if (dx < 0 || dx > size.width) {
        dx = random.nextDouble() * size.width;
      }
      if (dy < 0 || dy > size.height) {
        dy = random.nextDouble() * size.height;
      }
      element.postion = Offset(dx, dy);
    });

    list.forEach((element) {
      _paint.color = element.color;

      canvas.drawCircle(element.postion, element.radius, _paint);
    });
    // TODO: implement paint
  }


  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }

  Offset calculateXY(double speed, double theta) {
    return Offset(speed * cos(theta), speed * sin(theta));
  }
}

class BobbleBean {
  Offset postion;
  Color color;
  double speed;
  double theta;


  double radius;
}